var _page = $('.page');

//初始化popform
bottlecms.popform.init({
    submit: function (name, value, text, rfun) {
        var arr = {};
        arr[name] = value;
        updata_single_fun(arr, rfun);
    }
});

// 上传头像
$('input[name="photo"]').change(function (event) {
    $("#filedir").val("head_img");
    bottlecms.uploadfile({
        elem: this,
        success: function (pic) {
            updata_single_fun({
                photo: pic
            }, function () {
                $(".avatar .bg").css("background-image", "url(" + pic + ")");
                $(".avatar .imgbox .img img").attr('src', pic);
            })
        }
    })
});
// 上传相册
$('input[name="album"]').change(function (event) {
    $("#filedir").val("album");
    bottlecms.uploadfile({
        elem: this,
        success: function (pic) {
            updata_user_photo_fun(pic, function (data) {
                var obj = eval('(' + data.obj + ')');
                // var obj = data.obj.parseJSON();
                // var da = eval("'" + data.obj + "'");
                $(".photobox .addbtn").before('<li><img src="' + obj.photo + '"><i class="del" data-id="' + obj.id + '">×</i></li>')
            })
        }
    })
});


// // 上传音频
$('input[name="voice"]').change(function (event) {
    $("#filedir").val("voice");
    bottlecms.uploadfile({
        elem: this,
        maxsize: 60,
        success: function (voice) {
            updata_single_fun({
                voice: voice
            }, function (obj) {
                if (voice) {
                    $(".voicebox .bcui_voice audio").attr('src', voice);
                    $(".voicebox .bcui_voice").show();
                    $(".voicebox .placeholder").hide();
                }
            });
        }
    })
});

// // 上传视频
$('input[name="video"]').change(function (event) {
    $("#filedir").val("video");
    bottlecms.uploadfile({
        elem: this,
        maxsize: 60,
        success: function (video) {
            updata_single_fun({
                video: video
            }, function (obj) {
                if (video) {
                    $(".videobox").html('<video style="background: #000" width="300" height="200" controls="controls" preload="" src="' + video + '"></video>');
                }
            });
        }
    })
});

// 删除相册图片
$(_page).on('click', '.photobox li .del', function () {
    var _this = this;
    var id = $(this).attr('data-id');
    bottlecms.confirm("确定要删除吗？", function () {
        delete_user_photo_fun(id, function (data) {
            $(_this).parent().remove();
        })
    })
})

$(_page).on('click', '.js_up_recorder', function () {
    bottlecms.actions([
        [{
            text: '录音',
            onClick: function (modal, e) {
                if (typeof wx == "undefined") {
                    bottlecms.toast("仅支持在微信中使用");
                } else {
                    recorder_fun();
                }
            }
        }, {
            text: '本地上传',
            onClick: function (modal, e) {
                $('input[name="voice"]').trigger('click');
            }
        }],
        [{
            text: '取消',
            bg: 'danger'
        }]
    ]);
});
// 删除录音
$(_page).on('click', '.js_del_recorder', function () {
    var _this = this;
    bottlecms.confirm("确定要删除吗？", function () {
        updata_single_fun({
            voice: "null"
        }, function () {
            $(_this).parent().find('.bcui_voice').remove();
        });
    })
});
// 删除视频
$(_page).on('click', '.js_del_video', function () {
    var _this = this;
    bottlecms.confirm("确定要删除吗？", function () {
        updata_single_fun({
            video: "null"
        }, function () {
            $(_this).parent().parent().find('.videobox video').remove();
        });
    })
});